setwd("/Users/yaping/Documents/workspace/result/GNOME-seq/")
for (e in commandArgs(TRUE)) {
  ta = strsplit(e,"=",fixed=TRUE)
  	if(! is.na(ta[[1]][2])) {
		if(ta[[1]][1] == "k"){
			k<-ta[[1]][2]
			
		}
		if(ta[[1]][1] == "s"){
			s<-ta[[1]][2]
		}
		if(ta[[1]][1] == "f"){
			f<-ta[[1]][2]
		}
	}
}
gch<-read.table(k,sep="\t",header=F)
hcg<-read.table(s,sep="\t",header=F)
numElemGch<-length(gch[,1])
numHaveValueElemGch<-length(gch[!is.na(gch[,1]),][,1])
numElemHcg<-length(hcg[,1])
numHaveValueElemHcg<-length(hcg[!is.na(hcg[,1]),][,1])
scale<-seq(-2000,2000,by=20)
mainTitle=paste(f,"---numElem:",numElemGch,",","numHaveValueElemGch:",numHaveValueElemGch,";","numHaveValueElemHcg:",numHaveValueElemHcg)
pdf(paste(k,"pdf",sep="."),height=20, width=24, pointsize=12)
plot(scale,colMeans(hcg,na.rm=T),type="b",xlab="",ylab="",ylim=c(0.05,0.80),col=c("blue"),pch=16,lty=1,font=2,cex.axis=4,cex.lab=4,font.lab=2)
par(new=T)
plot(scale,colMeans(1-gch,na.rm=T),type="b",ylim=c(0.05,0.80),xlab="",ylab="",col=c("red"),pch=16,lty=1,font=2,main=mainTitle)
par(new=T)
#axis(1,at=seq(-2000,2000,by=200),pch=16,lty=1,font=2,cex.axis=3)
#axis(2,at=seq(0,1,by=0.1),pch=16,lty=1,font=2,cex.axis=3)
legend("bottomright",c("1-GCH","HCG"),col=c("red","blue"),pch=16,lty=1,cex=4)
abline(v=0)
dev.off()

numNoNaGch<-length(gch[!is.na(gch[,1]),][,1])/numElemGch
numNoNaHcg<-length(hcg[!is.na(hcg[,1]),][,1])/numElemGch

for(i in c(2:length(gch[1,]))){
	numNoNaGch<-c(numNoNaGch,length(gch[!is.na(gch[,i]),][,i])/numElemGch)
	numNoNaHcg<-c(numNoNaHcg,length(hcg[!is.na(hcg[,i]),][,i])/numElemGch)
}
pdf(paste(k,"noNAwin.pdf",sep="."),height=20, width=24, pointsize=12)
plot(scale,numNoNaGch,type="b",axes=FALSE,xlab="",ylab="",col=c("orange"),pch=16,lty=1,font=2,main=f)
par(new=T)
plot(scale,numNoNaHcg,type="b",xlab="",ylab="",ylim=c(0,1),col=c("green"),pch=16,lty=1,font=2,cex.axis=4,cex.lab=4,font.lab=2)
par(new=T)
#axis(1,at=seq(-2000,2000,by=200),pch=16,lty=1,font=2,cex.axis=3)
#axis(2,at=seq(0,1,by=0.1),pch=16,lty=1,font=2,cex.axis=3)
legend("topright",c("GCH windows no NA","HCG windows no NA"),col=c("orange","green"),pch=16,lty=1,cex=4)
dev.off()
